import { modal } from 'vue-strap'
import { showMsg, addInvester } from '../../../../../vuex/action.js'

export default {
  name: 'AddInvester',
  data() {
    return {
      showAddInvester: false,
      invester: {
        uid: '',
        name: '',
        img: '',
        org: '',
        cardNum: ''
      }
    }
  },
  methods: {
    validCheck() {
      let file = this.$els.investerImage.files[0]
      let inputs = ['org', 'cardNum']
      let key
      let invester = this.invester
      for (key of inputs) {
        if (!invester[key] || invester[key].length === 0 || !file) {
          return '* 标记的都为必填项'
        }
      }
    },
    evtAddInvester() {
      let validMsg = this.validCheck()
      if (validMsg) {
        return this.xShowMsg({
          type: 'danger',
          content: validMsg
        })
      }

      let formData = new FormData()
      Object.keys(this.invester).forEach((key) => {
        if (key === 'img') {
          formData.append('img', this.$els.investerImage.files[0])
        } else {
          formData.append(key, this.invester[key])
        }
      })

      this.xAddInvester(formData)
        .then(() => {
          this.$els.addForm.reset()
          this.showAddInvester = false
          this.$dispatch('on_refresh_list')
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  },
  vuex: {
    actions: {
      xShowMsg: showMsg,
      xAddInvester: addInvester
    }
  },
  components: {
    modal: modal
  },
  ready() {
  }
}
